package unit6.part7;

import java.util.Map;
import java.util.Random;

public class DrawReward {
    Map<String, Integer> rwdPool;// 奖池
    int count;// 可抽奖次数

    public DrawReward(Map<String, Integer> rwdPool) {
        this.rwdPool = rwdPool;
        for (Map.Entry<String, Integer> reward : rwdPool.entrySet()) {
            count += reward.getValue();
        }
    }

    void drAward() {// 抽奖
        if (count <= 0) {
            System.out.println("很遗憾没有奖品了");
        }
        Random random = new Random();
        int i = random.nextInt(count);// 0 ~ count-1
        // 0, 1~4, 5~10, 11~110
        int num = 0;
        for (Map.Entry<String, Integer> reward : rwdPool.entrySet()) {
            num += reward.getValue();
            if (i < num) { // 中奖了
                count--;
                reward.setValue(reward.getValue() - 1);
                System.out.println("恭喜您获得" + reward.getKey());
                break;
            }
        }
    }

    void showSurplus() {// 奖品信息
        for (Map.Entry<String, Integer> reward : rwdPool.entrySet()) {
            System.out.println(reward.getKey() + "；剩余" + reward.getValue());
        }
    }

}
